package Reflect.Exercise002;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * ClassName:Reflect
 * Package:Reflect.Exercise002
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/8-21:51
 * @Version:v1.0
 */
public class Reflect {
    /*反射可以跟配置文件结合的方式,动态的创建对象,并调用方法*/
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //读取配置文件的信息
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("E:\\IDER\\IDEAWorkCode\\IDEADemo\\prop.properties");
        prop.load(fis);
        fis.close();
        System.out.println(prop);

        //获取全类名和方法名
        String className = (String) prop.get("classname");
        String methodName = (String) prop.get("method");
        System.out.println(className);
        System.out.println(methodName);

        //利用反射创建对象并运行方法
        Class<?> clazz = Class.forName(className);

        //获取构造方法
        Constructor<?> con = clazz.getDeclaredConstructor();
        Object o = con.newInstance();
        System.out.println(o);

        //获取成员方法并运行
        Method method = clazz.getDeclaredMethod(methodName);
        method.setAccessible(true);
        method.invoke(o);


    }
}
